package com.NeuglsWorkStudio.UI.FormElement.Component
{
	import com.NeuglsWorkStudio.UI.FormElement.Config.TComboBoxConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TIconEditConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TPopupMenuConfig;
	import com.NeuglsWorkStudio.Utility.Utility;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	import org.osmf.layout.AbsoluteLayoutFacet;
	
	/**
	 * 实现一个ComboBox，即下拉框
	 * @author Neugls  07/06/2011**/
	public class TComboBox extends TComponent
	{
		private var _FItemIndex:int;
		private var _FPopList:TPopupMenu;
		private var _FIconEdit:TIconEdit
		
		public function TComboBox(AConfig:TConfig)
		{
			super(AConfig);
		}
		
		
		public function get ItemIndex():int
		{
			return _FItemIndex;
		}

		public function set ItemIndex(value:int):void
		{
			_FItemIndex = value;
		}

		public function ComboBoxConfig():TComboBoxConfig{
			return _FConfig as TComboBoxConfig;
		}
		
		override protected function Init():void{
			super.Init();
			
			var PopConfig:TPopupMenuConfig=new TPopupMenuConfig();
			PopConfig.AutoWidth=true;
			PopConfig.BackAlpha=ComboBoxConfig().BackAlpha;
			PopConfig.BackColor=ComboBoxConfig().BackColor;
			PopConfig.BorderColor=ComboBoxConfig().BorderColor;
			PopConfig.BorderSize=ComboBoxConfig().BorderSize;
			PopConfig.MenuItems=ComboBoxConfig().Items;
			PopConfig.Name='combopoplist';
			PopConfig.OnClickItem=OnUserClickedItem;
			PopConfig.RoundSize=5;
			PopConfig.MenuItemHaveIcon=false;
			
			_FPopList=new TPopupMenu(PopConfig);
			
			var triangleSize:Number=10;
			
			var DownS:Sprite=Utility.CreateTriangle(Utility.tdDown,triangleSize,ComboBoxConfig().IconColor);
			var t:Sprite=new Sprite();
			with(t.addChild(DownS)){
				x=DownS.width/2;
				y=DownS.height/2;
			}
		
			var DownIcon:BitmapData=new BitmapData(triangleSize,triangleSize,true,0<<28);
			//var m:Matrix=new Matrix(
			DownIcon.draw(t);
			t=null;
			DownS=null;
		
			
			var IconEditConfig:TIconEditConfig=new TIconEditConfig();
			IconEditConfig.BaseColor=ComboBoxConfig().BackColor;
			IconEditConfig.DisplayBorder=true;
			IconEditConfig.Enabled=true;
			IconEditConfig.Height=ComboBoxConfig().Height;
			IconEditConfig.Icon=new Bitmap(DownIcon);
			IconEditConfig.IconH=16;
			IconEditConfig.IconW=16;
			IconEditConfig.Name='iconcomm';
			IconEditConfig.Restrict='0-9%';
			IconEditConfig.OnEditorValueChanged=null;
			IconEditConfig.OnIconClick=DisplayItems;
			IconEditConfig.OnAcceptUserInput=FOnUserAccepted;
			IconEditConfig.Width=ComboBoxConfig().Width;
			
			_FIconEdit=new TIconEdit(IconEditConfig);
			this.addChild(_FIconEdit);
			
			_FItemIndex=ComboBoxConfig().DefaultItemIndex;
			_FIconEdit.Text=ComboBoxConfig().Items[ComboBoxConfig().DefaultItemIndex].Caption;
		}
		
		private function FOnUserAccepted(text:String):void{
			var n:Number=parseInt(text);
			if((n>0)&&(ComboBoxConfig().OnValueChanged!=null)){
				var E:Object=new Object();
				E.Size=n;
				ComboBoxConfig().OnValueChanged(-1,E);
			}
		}
		
		
		private function DisplayItems():void{
			if(stage!=null&&!stage.contains(_FPopList)){
				var p:Point=new Point(0,this.height+5);
				p=this.localToGlobal(p);
				var needRedoX:Boolean=false;
				var needRedoY:Boolean=false;
				if(p.y+_FPopList.height>stage.stageHeight)
					needRedoY=true;					
				if(p.x+_FPopList.width>stage.stageWidth)
					needRedoX=true;

				if(needRedoY){
					p.x=0;
					p.y=-3;
					
					p=this.localToGlobal(p);
					p.y-=_FPopList.height;
				}
				if(needRedoX)
					p.x=stage.stageWidth-_FPopList.width-5;
							
				stage.addChild(_FPopList);
				_FPopList.x=p.x<0?5:p.x;
				_FPopList.y=p.y;				
			}
		}
		
		private function OnUserClickedItem(ID:int,Extra:Object):void{
			_FItemIndex=ID;
			_FIconEdit.Text=ComboBoxConfig().Items[_FItemIndex].Caption;
			if(ComboBoxConfig().OnValueChanged!=null){
				ComboBoxConfig().OnValueChanged(ID,Extra);
			}
			_FPopList.ToDisapperar();
		}
		
		/**
		 * 获取Item信息
		 * @param ItemIndex 你要获取哪一项的信息
		 * @param Info 你需要的信息将会传给这个Info 对象。信息包括：<br/><code>
		 * Caption:String;ExtraData:Object
		 * </code>
		 * */
		public function GetItemInfo(ItemIndex:int,Info:Object):void{
			if(ItemIndex>-1&&ItemIndex<ComboBoxConfig().Items.length&&Info!=null){
				Info.Caption=ComboBoxConfig().Items[ItemIndex].Caption;
				Info.ExtraData=ComboBoxConfig().Items[ItemIndex].ExtraData;
			}
		}
		
		public function set Text(val:String):void{
			_FIconEdit.Text=val;
		}
		public function get Text():String{
			return _FIconEdit.Text;
		}
	}
}